﻿using GalaSoft.MvvmLight;
using System.Windows;
using System.Linq;
using System.Collections.ObjectModel;
using wp8nop.Model;
using System.Diagnostics;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System;
using wp8nop.Common;
using System.Collections.Generic;
using Microsoft.Practices.ServiceLocation;
using GalaSoft.MvvmLight.Messaging;
using System.Windows.Navigation;

namespace wp8nop.ViewModel
{
    /// <summary>
    /// This class contains properties that the main View can data bind to.
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public class MainViewModel : ViewModelBase
    {
        public event EventHandler<List<ProdottoViewModel>> OnVetrinaLoaded;
        public event EventHandler<ProdottoViewModel> OnOpenDettaglioProdotto;

        private readonly IDataService _dataService;

        #region PROPS

        /// <summary>
        /// The <see cref="IsLoading" /> property's name.
        /// </summary>
        public const string IsLoadingPropertyName = "IsLoading";

        private bool _isLoading = false;

        /// <summary>
        /// Sets and gets the IsLoading property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public bool IsLoading
        {
            get
            {
                return _isLoading;
            }

            set
            {
                if (_isLoading == value)
                {
                    return;
                }

                RaisePropertyChanging(IsLoadingPropertyName);
                _isLoading = value;
                RaisePropertyChanged(IsLoadingPropertyName);
            }
        }


        /// <summary>
        /// The <see cref="SelectedProduct" /> property's name.
        /// </summary>
        public const string SelectedProductPropertyName = "SelectedProduct";

        private ProdottoViewModel _selectedProduct = null;

        /// <summary>
        /// Sets and gets the SelectedProduct property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public ProdottoViewModel SelectedProduct
        {
            get
            {
                return _selectedProduct;
            }

            set
            {
                if (_selectedProduct == value)
                {
                    return;
                }

                RaisePropertyChanging(SelectedProductPropertyName);
                _selectedProduct = value;
                RaisePropertyChanged(SelectedProductPropertyName);
                if( SelectedProduct != null)
                    OpenDettaglioProdotto(SelectedProduct);
            }
        }

        /// <summary>
        /// The <see cref="SelectedCategory" /> property's name.
        /// </summary>
        public const string SelectedCategoryPropertyName = "SelectedCategory";

        private Categoria _selectedCategory = null;

        /// <summary>
        /// Sets and gets the SelectedCategory property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public Categoria SelectedCategory
        {
            get
            {
                return _selectedCategory;
            }

            set
            {
                if (_selectedCategory == value)
                {
                    return;
                }

                RaisePropertyChanging(SelectedCategoryPropertyName);
                _selectedCategory = value;
                RaisePropertyChanged(SelectedCategoryPropertyName);
          //      if( SelectedCategory != null )

            }
        }

        
        /// <summary>
        /// The <see cref="Categorie" /> property's name.
        /// </summary>
        public const string CategoriePropertyName = "Categorie";

        private List<Categoria> _categorie = null;

        /// <summary>
        /// Sets and gets the Categorie property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public List<Categoria> Categorie
        {
            get
            {
                return _categorie;
            }

            set
            {
                if (_categorie == value)
                {
                    return;
                }

                RaisePropertyChanging(CategoriePropertyName);
                _categorie = value;
                RaisePropertyChanged(CategoriePropertyName);
            }
        }


        /// <summary>
        /// The <see cref="Novita" /> property's name.
        /// </summary>
        public const string NovitaPropertyName = "Novita";

        private ObservableCollection<ProdottoViewModel> _novita = null;

        /// <summary>
        /// Sets and gets the Novita property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public ObservableCollection<ProdottoViewModel> Novita
        {
            get
            {
                return _novita;
            }

            set
            {
                if (_novita == value)
                {
                    return;
                }

                RaisePropertyChanging(NovitaPropertyName);
                _novita = value;
                RaisePropertyChanged(NovitaPropertyName);
            }
        }


        /// <summary>
        /// The <see cref="Vetrina" /> property's name.
        /// </summary>
        public const string MyPropertyPropertyName = "Vetrina";

        private List<ProdottoViewModel> _vetrina = null;

        /// <summary>
        /// Sets and gets the MyProperty property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public List<ProdottoViewModel> Vetrina
        {
            get
            {
                return _vetrina;
            }

            set
            {
                if (_vetrina == value)
                {
                    return;
                }

                RaisePropertyChanging(MyPropertyPropertyName);
                _vetrina = value;
                RaisePropertyChanged(MyPropertyPropertyName);
            }
        }
        #endregion
        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel(IDataService dataService)
        {
            _dataService = dataService;

            Categorie = _dataService.GetCategorie();


            try
            {

                IsLoading = true;

                _dataService.GetAllProductsDisplayedOnHomePage(
            (item, error) =>
            {
                IsLoading = false;
                if (error != null)
                {
                    Toast.SendToast("Verifica la tua connessione internet");
                    return;
                }
                Vetrina = item.Where(x => x.Model.groups.spotlight).Take(6).ToList();
                Novita = new ObservableCollection<ProdottoViewModel>();

                item.Where(x => !x.Model.groups.spotlight).ToList().ForEach(x => Novita.Add(x));

                Debug.WriteLine("Rotornati " + item.Count.ToString());
                if (OnVetrinaLoaded != null)
                    OnVetrinaLoaded(this, Vetrina);

                //  OnVetrinaLoaded(this, item.ToList());
                //      item.Where(x => x.groups.spotlight).ToList().ForEach(x => Vetrina.Add(x));
                Debug.WriteLine("Vetrina " + Vetrina.Count.ToString());

            });
            }
            catch (System.Exception ex)
            {
                IsLoading = false;

                Debug.WriteLine("Eccezione " + ex.Message);
            }
        }

        ////public override void Cleanup()
        ////{
        ////    // Clean up if needed

        ////    base.Cleanup();
        ////}

        internal void OpenDettaglioProdotto(ProdottoViewModel prod)
        {
            if (OnOpenDettaglioProdotto != null)
                OnOpenDettaglioProdotto(this, prod);
        }
    }
}